package cn.jekun.shop.app.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.ErpCartAttr;
import cn.jekun.component.shop.bean.Goods;
import cn.jekun.component.shop.service.CommonsCartService;
import cn.jekun.shop.common.service.CartService;


/**
 * 购物车(app)
 * @author Mr.Dong
 *
 */
@SuppressWarnings("serial")
public class CartAction extends AppBaseAction{
	@Resource(name = CartService.BEAN_NAME)
	protected CartService cartService;

	@Resource(name = CommonsCartService.BEAN_NAME)
	CommonsCartService commonsCartService;
	
	private List<Goods> goods  = new ArrayList<Goods>();

	public List<Goods> getGoods() {
		return goods;
	}

	public void setGoods(List<Goods> goods) {
		this.goods = goods;
	}
	
	/**
	 * 商品加入购物车(app)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String addshoptoCart(){
		JSONObject jsonObject = new JSONObject();
		try {
			String userId = request.getParameter("userId");
			String type = request.getParameter("type");
			//签名验证
			this.validateSign(getLoginSign(),userId,type);
			this.validateLoginApp(); //登录验证
			
			//购买方式,0、购物车方式购买 ，  1、立即购买
			if(StringUtils.isBlank(type)){ 
				type = Constants.CART_TYPE_ADD;  
			}
			List<ErpCartAttr> attrs = null;
			String sessionId = null;
			jsonObject = commonsCartService.addShopToCart2(userId, sessionId, type, goods, attrs);
            
			//购物车数量
			if (StringUtils.isNotBlank(userId)) {
				JSONObject cartNums = cartService.getCartShopNums(userId, null,null);
				jsonObject.put("cartNums", cartNums.getInt("nums"));
			}else{
				jsonObject.put("cartNums", 0);
			}
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "商品加入购物车成功，来源:mobile");//记录操作日志

			
		} catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "商品加入购物车失败，原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);	
	}

	/**
	 * 获取购物车商品数量
	 * @return
	 */
	public String getCartShopNum(){
        
		JSONObject jsonObject = new JSONObject();
		try {
			
			String userId = this.getUserId();
			this.validateSign(getLoginSign(),userId);
			this.validateLoginApp();//验证是否登录
			jsonObject = cartService.getCartShopNums(userId,null,null);
			
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}
		catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

	/**
	 * 获取购物车商品信息(列表)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getCartShops(){
		
		JSONObject jsonObject = new JSONObject();
		
		try {
			//签名验证
			this.validateSign(getLoginSign(),request.getParameter("userId"));
			
			this.validateLoginApp(); //验证是否登录
			
			formMap.put("userId", request.getParameter("userId"));
		
			jsonObject = cartService.getCartShops(formMap);
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}
		catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

	/**
	 * 修改购物车商品数量
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updateCartShopsNum(){
		JSONObject jsonObject = new JSONObject();
		try {
			//签名验证
			this.validateSign(getLoginSign(),request.getParameter("cartId"),request.getParameter("nums"));
			this.validateLoginApp(); //验证是否登录
			formMap.put("cartId", request.getParameter("cartId"));
			formMap.put("nums", request.getParameter("nums"));
			jsonObject = cartService.updateCartshopNum(formMap);

			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "修改购物车商品数量成功,购物车代码:"+request.getParameter("cartId")+",来源:mobile");//记录操作日志

		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "修改购物车商品数量失败,购物车代码:"+request.getParameter("cartId")+",原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志
		}
		catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

	/**
	 * 删除购物车商品
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String deleteCartShop(){
		JSONObject jsonObject = new JSONObject();
		try {
			//签名验证
			this.validateSign(getLoginSign(),request.getParameter("cartId"));
			this.validateLoginApp(); //验证是否登录
			formMap.put("cartId", request.getParameter("cartId"));
			jsonObject = cartService.deleteCartShops(formMap);
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "删除购物车商品数量成功,购物车代码:"+request.getParameter("cartId")+",来源:mobile");//记录操作日志

			
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "删除购物车商品失败,购物车代码:"+request.getParameter("cartId")+",原因:"+jsonObject.get("message")+",来源:mobile");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}


}
